package com.yjhb.android.sbz.activity;

import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.EverydayMoneyAdapter;
import com.yjhb.android.sbz.entity.RedPackDetails;
import com.yjhb.android.sbz.entity.RedPackItem;
import com.yjhb.android.sbz.mvp.everydaymoney.EverydayMoneyPresenter;
import com.yjhb.android.sbz.mvp.everydaymoney.EverydayMoneyView;
import com.yjhb.android.sbz.tool.DialogTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/4 9:10
 */
public class EverydayMoneyActivity extends BaseActivity<EverydayMoneyPresenter> implements EverydayMoneyView, BaseRecyclerAdapter.ItemClickListener<RedPackDetails>, OnRefreshLoadmoreListener {
    private final String TAG = "EverydayActivity";
    @BindView(R.id.emptyRecyclerView)
    EmptyRecyclerView emptyRecyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.emptyView)
    View emptyView;

    EverydayMoneyAdapter adapter;
    List<RedPackDetails> everydayMonies = new ArrayList<>();

    final int size = 25;
    int current = 1;

    @Override
    protected EverydayMoneyPresenter initPresenter() {
        return new EverydayMoneyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_everyday_money;
    }

    @Override
    protected void initUI() {
        emptyRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        emptyRecyclerView.setEmptyView(emptyView);
        emptyRecyclerView.setAdapter(adapter = new EverydayMoneyAdapter(this));
        adapter.setData(everydayMonies);
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @Override
    protected void initData() {
        loadingDialog.show();
        current = 1;
        presenter.getRedPackList(size, current);
    }

    @Override
    protected void regEvent() {
        adapter.setOnItemClick(this);
    }


    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getRedPackList(size, current);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        current++;
        presenter.getRedPackList(size, current);
    }

    /**
     * 打开红包弹窗
     */
    private Dialog showOpenRedPop(RedPackDetails redPackDetails) {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_red_pack, null, false);
        View close_iv = view.findViewById(R.id.close_iv);
        View pullDown = view.findViewById(R.id.pullDown);
        View more_tv = view.findViewById(R.id.more_tv);
        more_tv.setOnClickListener(v -> {
            open(redPackDetails);
        });
        final Dialog redPack = DialogTool.createDialog(this, view);
        close_iv.setOnClickListener(v -> redPack.dismiss());
        pullDown.setOnClickListener(v -> {
            presenter.pullRedPacket(redPackDetails);
            redPack.dismiss();
        });
        redPack.show();
        return redPack;
    }

    @Override
    public void onItemClick(int index, RedPackDetails redPackDetails) {
        presenter.queryPullState(redPackDetails);

    }

    @Override
    public void onViewClick(View view, String msg, int index, RedPackDetails redPackDetails) {

    }

    @Override
    public void onState(Integer state, RedPackDetails redPackDetails) {
        loadingDialog.cancel();
        if (state == 1) {
            open(redPackDetails);
        } else if (state == 0) {
            showOpenRedPop(redPackDetails);
        } else if (state == -1) {
//            ZxToast.showINFOBottomToast("您还没有做该任务哦~~");
            Tool.startEditTaskDetailActivity(this,redPackDetails.getTaskId());
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        ZxToast.showINFOBottomToast(msg);
    }

    @Override
    public void onLoad(List<RedPackDetails> data, boolean isRefresh) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        if (isRefresh) {
            everydayMonies.clear();
        }
        everydayMonies.addAll(data);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onOpenSuccess(RedPackDetails redPackDetails, RedPackItem redPackItem) {
        loadingDialog.cancel();
        open(redPackDetails);
    }

    private void open(RedPackDetails redPackDetails) {
        Tool.startRedDetail(getActivity(),redPackDetails);
    }


}
